"""
安装qiniu
    pip install qiniu
"""
from qiniu import Auth, put_file, etag
from mdpro.settings import BASE_DIR
import uuid


def qiniu_token(bucked_name):
    # 构建鉴权对象
    q = Auth(access_key='hmv--oUg0_UbsrqPX4_E-OlWsVRfYkpmmSa1b_bv',
             secret_key='9tW8bwz3VQonIUbuuAWa_Atpp8rayHZTv_oZGIrh')
    # 生成上传 Token
    token = q.upload_token(bucked_name)

    return token


def upload_img(bucked_name, file_path, domain_name):
    """
    收集本地信息到云服务器上
    参考地址：https://developer.qiniu.com/kodo/sdk/1242/python
    """
    # 指定上传空间，获取token
    token = qiniu_token(bucked_name)
    # 指定图片名称
    file_name = '{}.png'.format(uuid.uuid4())
    # 上传文件到七牛云
    ret, info = put_file(token,  file_name, file_path)
    # 拼接上传成功以后的图片外键链
    img_url = domain_name + ret.get('key')
    return img_url


if __name__ == '__main__':
    bucked_name = 'h2002a'
    file_path = '{}\\myutils\\123.png'.format(BASE_DIR)
    domain_name = 'http://qj5ps2dbi.hb-bkt.clouddn.com/'
    print(upload_img(bucked_name, file_path, domain_name))
